let gulp = require('gulp')
let uglify = require('gulp-uglify')
let rename = require('gulp-rename')
let minify = require('gulp-minify-css')
let connect = require('gulp-connect')
let watch = require('gulp-watch')
let less = require("gulp-less")
let concat = require('gulp-concat')
let imagemin = require('gulp-imagemin')
let pngquant = require('imagemin-pngquant')

gulp.task('uglifyjs', function() {
    gulp.src('src/**/*.js')
        .pipe(uglify())
        // .pipe(rename({suffix: '.min'}))
        .pipe(concat('all.min.js'))
        .pipe(gulp.dest('dist/js'))
})

gulp.task('cssminify', function() {
    gulp.src('src/**/*.css')
        .pipe(minify())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload())
})

gulp.task('html',function() {
    gulp.src('src/**/*.html')
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload())
})

gulp.task('less', function() {
    gulp.src('src/less/*.less')
        .pipe(less())
        .pipe(minify())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/css'))
        .pipe(connect.reload())
})


// 设置压缩参数
gulp.task('image', function() {
    gulp.src('src/image/**/**.*')
    .pipe(imagemin({
        optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
        progressive: true, //类型：Boolean 默认：false 无损压缩jpg图片
        interlaced: true, //类型：Boolean 默认：false 隔行扫描gif进行渲染
        multipass: true //类型：Boolean 默认：false 多次优化svg直到完全优化
    }))
    .pipe(gulp.dest('dist/image'))
    .pipe(connect.reload())
})



gulp.task("watch", function(){//监听任务
    watch("src/**/*.html", gulp.parallel('html',function() {
        console.log('html 拷贝成功')
    }));
    watch("src/**/*.css",gulp.parallel('cssminify',function() {
        console.log('css样式压缩成功')
    }));
    watch("src/**/*.js",gulp.parallel('uglifyjs',function() {
        console.log('js压缩成功')
    }));
    watch("src/less/*.less",gulp.parallel('less',function() {
        console.log('less编译成功')
    }));
    watch("src/image/**/*.*", gulp.parallel('image',function() {
        console.log('图片压缩成功')
    }))
})

gulp.task("server", function(){
	connect.server({
		root: "./dist",
		livereload: true,
		port: 8030
	})
})

gulp.task('default',gulp.parallel('watch','server',function() {
    console.log('操作成功')
}))
